//
//  SEGPageSizeCustomerFlowLayout.m
//  SEGUhomePropertyBaseService
//
//  Created by Clavis on 2018/12/25.
//  Copyright © 2018 刘山. All rights reserved.
//

#import "SEGPageSizeCustomerFlowLayout.h"
#if __has_include(<Common/SEGLayoutMacro.h>)
#import <Common/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

@implementation SEGPageSizeCustomerFlowLayout

- (void)prepareLayout {
    [super prepareLayout];
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    // 修改内边距（让第一个和最后一个cell能居中）
    CGFloat inset = (LYScreenWidth() - self.itemSize.width) * 0.5;
    self.sectionInset = UIEdgeInsetsMake(0, inset, 0, inset);
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    //先用super调用此方法，获取super已经计算好的所有布局属性（原本的样式）
    rect.origin.x -= self.collectionView.ly_width * 2;
    rect.origin.x = rect.origin.x < 0 ? 0 : rect.origin.x;
    rect.size.width += self.collectionView.ly_width * 2;
    
    NSArray *array = [[NSArray alloc] initWithArray:[super layoutAttributesForElementsInRect:rect] copyItems:YES];
    
    //计算此刻 collectionView窗口的中心点 在 collectionView的contentSize上 相对距离的x点
    CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width * 0.5;
    
    //在super计算好的布局属性的基础上，进行修改
    for (UICollectionViewLayoutAttributes *attri in array) {
        
        //cell的中心点x此刻与collectionView窗口的中心点的间距
        CGFloat delta = ABS(attri.center.x - centerX);
        
        //根据间距值计算cell的缩放比例
        CGFloat scale = 1 - 0.2 * (delta / self.collectionView.ly_width);
        CGFloat scaleW = 1 - 0.15 * (delta / self.collectionView.ly_width);
        CGFloat alpha = 1 - 0.4 * (delta / self.collectionView.ly_width);
        if (self.type == SEGPageSizeCustomerFlowLayoutType_NeighborDaily) {
            scaleW = 1;
            alpha = 1;
        } else if (self.type == SEGPageSizeCustomerFlowLayoutType_ServiceHotline) {
            scale = 1 - 0.33 * (delta / self.collectionView.ly_width);
            scaleW = 1;
        }
        attri.transform = CGAffineTransformMakeScale(scaleW, scale);
        attri.alpha = alpha;
    }
    
    return array;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}

@end
